package com.ty.erpend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.erpend.entity.Warehouse;
import com.ty.erpend.resp.Result;
import com.ty.erpend.service.IWarehouseService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author txy
 * @since 2024-02-19
 */
@RestController
@CrossOrigin
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    private IWarehouseService warehouseService;

    @GetMapping("/getWarehouse")
    public Result getWarehouses(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                @RequestParam(value = "size",defaultValue = "5") Integer size){
        IPage<Warehouse> page1 = new Page<>(page, size);
        List<Warehouse> list = warehouseService.list(page1);
        Result result = new Result();
        result.setData(list);
        result.setTotal(page1.getTotal());
        return result;
    }
    @PostMapping("/addWarehouse")
    public Result addWarehouse(Warehouse warehouse){
        boolean save = warehouseService.save(warehouse);
        Result result = new Result();
        result.setSuccess(save);
        return result;
    }
    @PostMapping("/updateWarehouse")
    public Result updateWarehouse(Warehouse warehouse){
        boolean update = warehouseService.updateById(warehouse);
        Result result = new Result();
        result.setSuccess(update);
        return result;
    }
    @PostMapping("/deleteWarehouse")
    public Result deleteWarehouse(Integer id){
        boolean remove = warehouseService.removeById(id);
        Result result = new Result();
        result.setSuccess(remove);
        return result;
    }
    @PostMapping("/deleteWarehouses")
    public Result deleteWarehouses(String ids){
        String[] split = ids.split(",");
        boolean remove = warehouseService.removeByIds(Arrays.asList(split));
        Result result = new Result();
        result.setSuccess(remove);
        return result;
    }
}
